#/usr/bin/env python

import wx

class SSHFrame( wx.Frame ):

    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=wx.Size(w=800,h=600), style=wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.SYSTEM_MENU)
        self.statusbar = self.CreateStatusBar()
        self.version   = "SSHER 0.1"
        #self.SetGlobalStatusMessage("Started")

        # Armado del menu
        self.DrawMenu()

        self.Bind(wx.EVT_MENU, self.OnQuit, self.quit_item) 

    def SetGlobalStatusMessage(self, msg):
        self.statusbar.SetStatusText("%s --- %s" % (self.version, msg))

    def DrawMenu( self ):
        self.menu = wx.MenuBar()
        filemenu  = wx.Menu()
        self.new_item  = filemenu.Append(-1, 'New..', 'Add a new ssh connection')
        self.quit_item = filemenu.Append(wx.ID_EXIT, 'Quit', 'Quit SSHER')
        self.menu.Append(filemenu, '&File')
        self.SetMenuBar(self.menu)
        self.Show(True)

    def OnQuit(self, e):
        self.Close()

class SSHERTaskBarIcon( wx.TaskBarIcon ):
    
    ID_HELLO  = wx.NewId()
    ID_HELLO2 = wx.NewId()

    def __init__(self, instance):
        super(SSHERTaskBarIcon, self).__init__()

        app_icon = wx.Icon("icon.ico", wx.BITMAP_TYPE_ICO)
        self.SetIcon(app_icon)
        self.instance = instance

        self.Bind(wx.EVT_MENU, self.OnMenu)
        self.Bind(wx.EVT_MENU, self.OnClose, id=wx.ID_CLOSE)
        self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.OnClick)

    def OnClick(self, event):
        if self.instance.IsShown():
            self.instance.Hide()
        else:
            self.instance.SetFocus()
            self.instance.Show()

    def OnMenu(self, event):
        evt_id = event.GetId()

        if   evt_id == SSHERTaskBarIcon.ID_HELLO:
            self.instance.SetFocus()
            self.instance.Show()
        elif evt_id == SSHERTaskBarIcon.ID_HELLO2:
            self.instance.Hide()
        elif evt_id == wx.ID_CLOSE:
            self.instance.Destroy()
            self.Destroy()
            app.Exit()
        else:
            event.Skip()

    def OnClose(self, event):
        SSHER.Exit()

class SSHER( wx.App ):

    def OnInit( self ):
        frame = SSHFrame(None, -1, "SSHER")
        frame.Hide()

        self.SetTopWindow( frame ) 
        self.SSHERTaskBar = SSHERTaskBarIcon(self)

        return True
